<script lang="ts">
import { defineComponent, ref, onMounted, onUnmounted, inject } from 'vue'
import { Pagination, FreeMode, Autoplay, Virtual } from 'swiper'
import { Swiper, SwiperSlide } from 'swiper/vue'
import router from "@/router";
import 'swiper/css'
import 'swiper/css/pagination'
import 'swiper/css/free-mode'
import 'swiper/css/virtual'
import resizeRank from '@/util/screen'

export default defineComponent({
  name: 'swiper-example-free-mode',
  title: 'Free mode / no fixed positions',
  url: import.meta.url,
  components: {
    Swiper,
    SwiperSlide
  },
  props: {
    solutionData: Array
  },
  setup(props: any) {
    const checkIndex = ref<number>(0)
    const rank = ref(0)
    let activeWidth = ref(0)
    let bannerLength = ref(0)
    let timed = ref() as any

    const myFn = inject<any>("open")
    props.solutionData?.forEach((item: any, index: any) => {
      if (index == 0) {
        item.active = true
      } else {
        item.active = false
      }
    })
    const solutionData = props.solutionData
    bannerLength.value = 24 / (solutionData.length)
    const toCheckSulution = (item: any, index: number) => {
      solutionData.map((it: any, ind: any) => {
        it.check = false
      })
      item.check = true
      checkIndex.value = index
      
      activeWidth.value = 0
      solutionData.forEach((item2: any, index2: any) => {
        if(item2.id === item.id) {
          item2.active = true
        }else{
          item2.active = false
        }
      })
    }
    const toSolution = () => {
      myFn()
    }
    const onSlideChange = (swiper: any) => {
      solutionData.map((it: any, ind: any) => {
        it.check = false
        if (ind == swiper.activeIndex) {
          it.check = true
        }
      })
      checkIndex.value = swiper.activeIndex
    }
    const onSlideNext = (val: any) => {
      activeWidth.value = 0
      solutionData.forEach((item: any, index: any) => {
        if (index == val.realIndex) {
          item.active = true
        } else {
          item.active = false
        }
      })
    }
    const co = () => {
      if (activeWidth.value == 100) {
        return
      } else {
        activeWidth.value += 2
      }
    }
    onMounted(() => {
      /**
       * @methods 判定pc&movie
       */
      rank.value = resizeRank(rank.value)
      window.addEventListener('resize', () => {
        let screenWidth = document.body.clientWidth
        if (screenWidth < 768) {
          rank.value = 1
        } else {
          rank.value = 0
        }
      })
      timed.value = setInterval(co, 100)
    })
    onUnmounted(() => {
      clearInterval(timed.value)
    })
    return {
      toCheckSulution,
      toSolution,
      checkIndex,
      solutionData,
      modules: [Pagination, FreeMode, Autoplay, Virtual],
      rank,
      bannerLength,
      activeWidth,
      onSlideChange,
      onSlideNext
    }
  }
})
</script>

<template>
  <div>
    <div class="content">
      <el-row class="solution">
        <el-col class="hiddon_box" :xl="7" :lg="7" :md="7" :sm="7" :xs="0">
          <div class="solutionLeft">
            <div class="secondTitle solutionLeftTitle">{{ solutionData[checkIndex].name }}</div>
            <div class="contentText solutionLeftDemand">{{ solutionData[checkIndex].desc }}</div>
            <el-button color="#064DD6" size="large" round @click="toSolution()">马上咨询</el-button>
            <div class="content bannerPagenationBox">
            <div class="bannerPagenation">
              <el-row :gutter="10" style="width:100%">
                <el-col :span="bannerLength" v-for="item in solutionData" :key="item.id">
                  <div :class="item.active ? 'active' : 'pagenation'">
                    <div :class="item.active ? 'activeDiv' : ''" :style="{ 'width': activeWidth + '%' }"></div>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
          </div>
          <div class="voe_box"></div>
          
        </el-col>
        <el-col :xl="17" :lg="17" :md="17" :sm="17" :xs="24">
          <div class="solutionRight">
            <!-- <swiper class="swiper" :modules="modules" :space-between="rank == 0 ? 30 : 0"
              :slides-per-view="rank == 0 ? 3 : 1"  :free-mode="true" :autoplay="{
                delay: 5000,
                stopOnLastSlide: false,
                disableOnInteraction: false,
              }" :pagination="{ clickable: true }" @slideChange="onSlideChange"
              @slideChangeTransitionStart="onSlideNext">
              <swiper-slide class="slide " v-for="(item, index) in solutionData" :key="index"
                :class="item.check ? 'slideCheck' : 'slide'" @click="toCheckSulution(item, index)">  
                <div class="" v-if="rank == 0">
                  <img :src="item.cover_image" alt="" :class="item.check ? 'ImgCheck' : 'ImgNoCheck'">
                </div>
                <div class="" v-else>
                  <img :src="item.cover_image" alt="" :class="item.check ? 'ImgNoCheck' : 'ImgNoCheck'">
                </div>
                <el-col :xl="0" :lg="0" :md="0" :sm="0" :xs="24">
                  <div class="solutionLeft">
                    <div class="secondTitle solutionLeftTitle" style="margin-top: 0px;">{{ item.name }}</div>
                    <el-button style="margin-top: 20px;" color='#064DD6' size="large" type="primary" round
                      class="solutionLeftBtn" @click="toSolution()">马上咨询
                    </el-button>
                  </div>
                </el-col>
              </swiper-slide>
            </swiper> -->
            <swiper class="swiper" :modules="modules" :space-between="rank == 0 ? 30 : 0"
              :slides-per-view="rank == 0 ? 3 : 1"  :free-mode="true" :autoplay="{
                delay: 5000,
                stopOnLastSlide: false,
                disableOnInteraction: false,
              }" :pagination="{ clickable: true }" :centered-slides="true" :virtual="true" @slideChange="onSlideChange"
              @slideChangeTransitionStart="onSlideNext">
              <swiper-slide class="slide " v-for="(item, index) in solutionData" :key="index"
                :class="item.check ? 'slideCheck' : 'slide'">  
                <div class="" v-if="rank == 0">
                  <img :src="item.cover_image" alt="" :class="item.check ? 'ImgCheck' : 'ImgNoCheck'">
                </div>
                <div class="" v-else>
                  <img :src="item.cover_image" alt="" :class="item.check ? 'ImgNoCheck' : 'ImgNoCheck'">
                </div>
                <el-col :xl="0" :lg="0" :md="0" :sm="0" :xs="24">
                  <div class="solutionLeft">
                    <div class="secondTitle solutionLeftTitle" style="margin-top: 0px;">{{ item.name }}</div>
                    <el-button style="margin-top: 20px;" color='#064DD6' size="large" type="primary" round
                      class="solutionLeftBtn" @click="toSolution()">马上咨询
                    </el-button>
                  </div>
                </el-col>
              </swiper-slide>
            </swiper>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import url("@/assets/css/content.css");

.secondaryContent {
  height: 380px;
}

.hiddon_box {
  z-index: 2;
  background-color: #fff;
  position: relative;

  .voe_box {
    width: 100vh;
    height: 100%;
    position: absolute;
    right: 100%;
    top: 0;
    background-color: #fff;
  }
}

.solution {
  padding-top: 20px;
  display: flex;


  .solutionLeft {
    height: 100%;
    padding: 20px 40px 60px 0px;

    .solutionLeftTitle {
      margin-top: 40px;
    }

    .solutionLeftDemand {
      margin-top: 20px;
      margin-bottom: 30px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }

  }

  :deep(.solutionRight) {
    height: 100%;

    .swiper {
      overflow: inherit;

      .swiper-pagination {
        bottom: -50px !important;
      }
    }


    img {
      border-radius: 15px;
      width: 100%;
      height: 360px;
      object-fit: cover;
    }

    .ImgCheck {
      border: 5px solid $mainColor;
    }
  }
}

.bannerPagenationBox {
  margin: 30px;

  .bannerPagenation {
    position: absolute;
    left: 0%;
    z-index: 999;
    width: 280px;
    height: 4px;
    margin: 0 auto;
    display: flex;
    justify-content: center;

    .pagenation {
      background-color: #666;
      height: 100%;
      opacity: 0.4;
      width: 100%;
      border-radius: 100px;
    }

    .active {
      width: 100%;
      height: 100%;
      background-color: $auxiliaryColor3;
      opacity: 1;
      border-radius: 100px;

      .activeDiv {
        width: 0%;
        height: 100%;
        background-color: $mainColor;
      }
    }
  }

}

@media screen and (max-width: 1200px) {
  .solution {
    .solutionLeft {
      .solutionLeftTitle {
        margin-top: 0px;
      }
    }
  }
}

@media screen and (max-width:450px) {
  .secondaryContent {
    height: initial;
  }

  .solutionLeft {
    padding: 20px !important;
    text-align: center;
  }

  .solutionRight {

    img {
      height: 220px !important;
    }
  }

  :deep(.swiper-pagination) {
    bottom: 0px !important;
  }

  :deep(.swiper) {
    .swiper-pagination {
      bottom: -50px !important;
      display: none;
    }

    .slide {
      padding-right: 30px;
    }
  }
}
</style>